<template>
  <div class="container">
    <div class="auth-box">
      <h1>登录</h1>
      
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="80px"
        class="form-container"
      >
        <el-form-item label="用户名" prop="username">
          <el-input 
            v-model="formData.username" 
            placeholder="请输入用户名"
          />
        </el-form-item>
        
        <el-form-item label="密码" prop="password">
          <el-input 
            v-model="formData.password" 
            type="password" 
            placeholder="请输入密码"
          />
        </el-form-item>
        
        <el-form-item class="button-group">
          <el-button 
            type="primary" 
            @click="submitForm"
            class="auth-button action-btn"
          >
            登录
          </el-button>
          
          <el-button 
            @click="resetForm"
            class="auth-button reset-btn"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue';
import request from '../utils/request';
import { ElMessage } from 'element-plus';
import { useRouter } from 'vue-router';

const router = useRouter();
const formRef = ref();

const formData = reactive({
  username: '',
  password: ''
});

// 表单验证规则
const rules = reactive({
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ]
});

const resetForm = () => {
  formRef.value.resetFields();
};

const submitForm = () => {
  formRef.value.validate(async (valid) => {
    if (!valid) {
      ElMessage.error('请填写正确的表单信息');
      return;
    }

    try {
      const res = await request.post(`user/login?username=${formData.username}&password=${formData.password}`);
      
      console.log('API响应:', res.data);
      
      if (res.data.code === 200) {
        ElMessage.success(res.data.info);
        
        // 登录成功后存储token并跳转
        window.sessionStorage.setItem('token', res.data.token);
        router.push({ name: 'home' });
      } else {
        ElMessage.error(res.data.info || '登录失败');
      }
    } catch (error) {
      console.error('API请求错误:', error);
      ElMessage.error('登录请求失败');
    }
  });
};
</script>

<style scoped>
.container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-size: cover;
}

.auth-box {
  background-color: rgba(255, 255, 255, 0.95);
  padding: 2.5rem;
  border-radius: 15px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.2);
  width: 450px;
  backdrop-filter: blur(5px);
}

h1 {
  color: #2c3e50;
  margin-bottom: 2rem;
  font-size: 1.8rem;
  font-weight: 600;
  text-align: center;
}

.form-container {
  margin-top: 20px;
}

.button-group {
  display: flex;
  justify-content: center;
  margin-top: 30px;
  gap: 50px; /* 增大按钮间距 */
}

.auth-button {
  padding: 10px 20px;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.auth-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
}

.action-btn {
  background-color: #3498db;
  color: white;
}

.action-btn:hover {
  background-color: #2980b9;
}

.reset-btn {
  background-color: #e74c3c;
  color: white;
}

.reset-btn:hover {
  background-color: #c0392b;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .auth-box {
    width: 90%;
    padding: 1.5rem;
  }
  
  .button-group {
    flex-direction: column;
    gap: 10px;
  }
  
  .auth-button {
    width: 100%;
  }
}
</style>